package net.xju3_4.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.Car;
import net.xju3_4.entity.Merchant;
import net.xju3_4.service.MerchantService;
import net.xju3_4.service.UserProfileService;
import net.xju3_4.service.impl.MerchantServiceImpl;
import net.xju3_4.service.impl.UserProfileServiceImpl;

import java.io.IOException;


@RequestMapping("/api/merchant")
public class MerchantController {

    private MerchantService merchantService = new MerchantServiceImpl();

    @RequestMapping("/update.do")
    public void updateMerchant(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Merchant merchant = new Merchant();
        request.setCharacterEncoding("utf-8");
        merchant.setId(Integer.parseInt(request.getParameter("id")));
        merchant.setMerchantName(request.getParameter("merchantName"));
        merchant.setContactPerson(request.getParameter("contactPerson"));
        merchant.setContactPhone(request.getParameter("contactPhone"));
        merchant.setAddress(request.getParameter("address"));
        merchant.setBusinessLicense(request.getParameter("businessLicense"));
        merchant.setDescription(request.getParameter("description"));
        merchant.setLogoUrl(request.getParameter("logoUrl"));
        merchantService.update(merchant);
        JSONUtil.printByJSON(response, Result.success("更新成功"));
    }



    @RequestMapping("/get.do")
    public void getMerchant(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        Merchant merchant = merchantService.getMerchant(id);
        JSONUtil.printByJSON(response, Result.success(merchant));
    }

    @RequestMapping("/resetPassword.do")
    public void resetPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Merchant merchant = new Merchant();
        merchant.setId(Integer.parseInt(request.getParameter("id")));
        merchant.setPassword(request.getParameter("password"));
        merchantService.resetPassword(merchant);
        JSONUtil.printByJSON(response, Result.success("修改成功"));
    }

    @RequestMapping("/uploadImage.do")
    public void uploadImage(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");

            String id = req.getParameter("id");
            String logoUrl = req.getParameter("logoUrl");

            if (id == null || logoUrl == null) {
                JSONUtil.printByJSON(resp, Result.fail("参数不完整"));
                return;
            }
            boolean success = merchantService.updateLogo(id, logoUrl);
            if (success) {
                JSONUtil.printByJSON(resp, Result.success("头像更新成功"));
            } else {
                JSONUtil.printByJSON(resp, Result.fail("头像更新失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(resp, Result.fail("系统错误：" + e.getMessage()));
        }
    }



}
